package com.zysdev.zysplayer.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;


import com.zysdev.zysplayer.player.ZYSPlayer;
import com.zysdev.zysplayer.player.listener.OnPlayerListener;

import java.util.ArrayList;
import java.util.List;

public class PlayMusicService extends Service implements MediaPlayer.OnCompletionListener, OnPlayerListener {
    private static final String TAG = "David";
    private ZYSPlayer mPlayer;
    /*操作指令*/
    public static final String ACTION_OPT_MUSIC_PLAY = "ACTION_OPT_MUSIC_PLAY";
    public static final String ACTION_OPT_MUSIC_PAUSE = "ACTION_OPT_MUSIC_PAUSE";
    public static final String ACTION_OPT_MUSIC_RESUME = "ACTION_OPT_MUSIC_RESUME";
    public static final String ACTION_OPT_MUSIC_NEXT = "ACTION_OPT_MUSIC_NEXT";
    public static final String ACTION_OPT_MUSIC_LAST = "ACTION_OPT_MUSIC_LAST";
    public static final String ACTION_OPT_MUSIC_SEEK_TO = "ACTION_OPT_MUSIC_SEEK_TO";
    public static final String ACTION_OPT_MUSIC_LEFT = "ACTION_OPT_MUSIC_LEFT";
    public static final String ACTION_OPT_MUSIC_RIGHT = "ACTION_OPT_MUSIC_RIGHT";
    public static final String ACTION_OPT_MUSIC_CENTER = "ACTION_OPT_MUSIC_CENTER";
    public static final String ACTION_OPT_MUSIC_VOLUME = "ACTION_OPT_MUSIC_VOLUME";
    public static final String ACTION_OPT_MUSIC_SPEED = "ACTION_OPT_MUSIC_SPEED";
    public static final String ACTION_OPT_MUSIC_SPEED_ADD = "ACTION_OPT_MUSIC_SPEED_ADD";
    public static final String ACTION_OPT_MUSIC_PITCH = "ACTION_OPT_MUSIC_PITCH";
    public static final String ACTION_OPT_MUSIC_PITCH_ADD = "ACTION_OPT_MUSIC_PITCH_ADD";
    public static final String ACTION_OPT_MUSIC_PITCH_SPEED_NORMAL = "ACTION_OPT_MUSIC_PITCH_SPEED_NORMAL";
    public static final String ACTION_OPT_MUSIC_RELEASE = "ACTION_OPT_MUSIC_RELEASE";
    /*状态指令*/
    public static final String ACTION_STATUS_MUSIC_PLAY = "ACTION_STATUS_MUSIC_PLAY";
    public static final String ACTION_STATUS_MUSIC_PAUSE = "ACTION_STATUS_MUSIC_PAUSE";
    public static final String ACTION_STATUS_MUSIC_COMPLETE = "ACTION_STATUS_MUSIC_COMPLETE";
    public static final String ACTION_STATUS_MUSIC_DURATION = "ACTION_STATUS_MUSIC_DURATION";
    public static final String ACTION_STATUS_MUSIC_PLAYER_TIME = "ACTION_STATUS_MUSIC_PLAYER_TIME";
    public static final String PARAM_MUSIC_DURATION = "PARAM_MUSIC_DURATION";
    public static final String PARAM_MUSIC_SEEK_TO = "PARAM_MUSIC_SEEK_TO";
    public static final String PARAM_MUSIC_CURRENT_POSITION = "PARAM_MUSIC_CURRENT_POSITION";
    public static final String PARAM_MUSIC_IS_OVER = "PARAM_MUSIC_IS_OVER";

    private int mCurrentMusicIndex = 0;
    private MusicReceiver mMusicReceiver = new MusicReceiver();

    private String[] urls;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initBoardCastReceiver();
        mPlayer = new ZYSPlayer();
        mPlayer.setOnPlayerListener(this);
        urls = new String[6];
        urls[0] = "http://downsc.chinaz.net/Files/DownLoad/sound1/201906/11582.mp3";
        urls[1] = "rtmp://58.200.131.2:1935/livetv/cctv1";
        urls[2] = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
        urls[3] = "http://vjs.zencdn.net/v/oceans.mp4";
        urls[4] = "http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4";
        urls[5] = "http://mpge.5nd.com/2015/2015-11-26/69708/1.mp3";
        mPlayer.setSource(urls[5]);
    }

    private void initBoardCastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_OPT_MUSIC_PLAY);
        intentFilter.addAction(ACTION_OPT_MUSIC_PAUSE);
        intentFilter.addAction(ACTION_OPT_MUSIC_RESUME);
        intentFilter.addAction(ACTION_OPT_MUSIC_NEXT);
        intentFilter.addAction(ACTION_OPT_MUSIC_LAST);
        intentFilter.addAction(ACTION_OPT_MUSIC_SEEK_TO);
        intentFilter.addAction(ACTION_OPT_MUSIC_LEFT);
        intentFilter.addAction(ACTION_OPT_MUSIC_RIGHT);
        intentFilter.addAction(ACTION_OPT_MUSIC_VOLUME);
        intentFilter.addAction(ACTION_OPT_MUSIC_CENTER);
        intentFilter.addAction(ACTION_OPT_MUSIC_SPEED);
        intentFilter.addAction(ACTION_OPT_MUSIC_SPEED_ADD);
        intentFilter.addAction(ACTION_OPT_MUSIC_PITCH);
        intentFilter.addAction(ACTION_OPT_MUSIC_PITCH_ADD);
        intentFilter.addAction(ACTION_OPT_MUSIC_PITCH_SPEED_NORMAL);
        intentFilter.addAction(ACTION_OPT_MUSIC_RELEASE);
        LocalBroadcastManager.getInstance(this).registerReceiver(mMusicReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mMusicReceiver);
    }

    //监听
    private void play(final int index) {
        mPlayer.prepare();
    }

    private void pause() {
        mPlayer.pause();
    }

    private void resume() {
        mPlayer.resume();
    }

    private void stop() {
    }

    private void next() {
    }

    private void last() {

    }

    private void seekTo(int position) {
        mPlayer.seekTo(position);
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
    }


    @Override
    public void onPrepared(int total) {
        Log.e("---", "---onPrepared  total=" + total);
        Intent intent = new Intent(ACTION_STATUS_MUSIC_PLAYER_TIME);
        intent.putExtra("currentTime", 0);
        intent.putExtra("totalTime", total);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        mPlayer.play();
    }

    @Override
    public void onCurrentTime(int currentTime, int totalTime) {
        Intent intent = new Intent(ACTION_STATUS_MUSIC_PLAYER_TIME);
        intent.putExtra("currentTime", currentTime);
        intent.putExtra("totalTime", totalTime);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    @Override
    public void onLoading(boolean loading) {

    }


    class MusicReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.i(TAG, "onReceive: " + action);
            if (action.equals(ACTION_OPT_MUSIC_PLAY)) {
                play(mCurrentMusicIndex);
            } else if (action.equals(ACTION_OPT_MUSIC_LAST)) {
                last();
            } else if (action.equals(ACTION_OPT_MUSIC_NEXT)) {
                next();
            } else if (action.equals(ACTION_OPT_MUSIC_SEEK_TO)) {
                int position = intent.getIntExtra(PlayMusicService.PARAM_MUSIC_SEEK_TO, 0);
                seekTo(position);
            } else if (action.equals(ACTION_OPT_MUSIC_RESUME)) {

                resume();
            } else if (action.equals(ACTION_OPT_MUSIC_PAUSE)) {
                pause();
            } else if (action.equals(ACTION_OPT_MUSIC_RIGHT)) {
                mPlayer.setMute(0);
            } else if (action.equals(ACTION_OPT_MUSIC_LEFT)) {
                mPlayer.setMute(1);
            } else if (action.equals(ACTION_OPT_MUSIC_CENTER)) {
                mPlayer.setMute(2);
            } else if (action.equals(ACTION_OPT_MUSIC_VOLUME)) {
                mPlayer.setVolume(i++);
                Log.i(TAG, "onReceive: " + i);
            } else if (action.equals(ACTION_OPT_MUSIC_SPEED)) {
                Log.e("---","-----ACTION_OPT_MUSIC_SPEED");

                //减速
                speed = speed - 0.1f;
                if (speed <= 0) {
                    speed = 0.1f;
                }
                mPlayer.setSpeed(speed);
            } else if (action.equals(ACTION_OPT_MUSIC_SPEED_ADD)) {
                Log.e("---","-----ACTION_OPT_MUSIC_SPEED_ADD");

                //减速
                speed = speed + 0.1f;
                if (speed > 2f) {
                    speed = 2f;
                }
                mPlayer.setSpeed(speed);
            } else if (action.equals(ACTION_OPT_MUSIC_PITCH)) {
                Log.e("---","-----ACTION_OPT_MUSIC_PITCH");

                //减速
                pitch = pitch - 0.1f;
                if (pitch <= 0f) {
                    pitch = 0.1f;
                }
                mPlayer.setPitch(pitch);
            } else if (action.equals(ACTION_OPT_MUSIC_PITCH_ADD)) {
                Log.e("---","-----ACTION_OPT_MUSIC_PITCH_ADD");

                //减速
                pitch = pitch + 0.1f;
                if (pitch > 2f) {
                    pitch = 2f;
                }
                mPlayer.setPitch(pitch);
            }else if (action.equals(ACTION_OPT_MUSIC_PITCH_SPEED_NORMAL)) {
                Log.e("---","-----ACTION_OPT_MUSIC_PITCH_SPEED_NORMAL");
                //减速
                speed = 1.0f;
                pitch = 1.0f;
                mPlayer.setSpeed(speed);
                mPlayer.setPitch(pitch);
            }else if (action.equals(ACTION_OPT_MUSIC_RELEASE)) {
                Log.e("---","-----ACTION_OPT_MUSIC_RELEASE");

                mPlayer.stop();
            }
        }
    }

    int i = 0;
    float speed = 1.0f;
    float pitch = 1.0f;
}
